﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class ManageUserRoles : SOSecurityPage
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                if (!IsPostBack)
                {
                    SetDataSourceToTypes(structureFacade.GetUserTypes(""));
                    if (!string.IsNullOrEmpty(Request.QueryString["usertype"]))
                        ddlRoleType.SelectedValue = Request.QueryString["usertype"];
                }

                lblError.ForeColor = Color.Black;
                lblError.Text = "";
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void btnCreateRole_Click(object sender, EventArgs e)
        {
            try
            {
                if (ddlRoleType.SelectedIndex > 0)
                {
                    if (!Roles.RoleExists(txtRole.Text))
                    {
                        Roles.CreateRole(txtRole.Text);
                    }

                    /*Get Role by name*/
                    UserRole_aspnet userRole_aspnet = structureFacade.GetAspnetUserRole(txtRole.Text);

                    /*Get ID of the record*/
                    Guid roleID = userRole_aspnet.RoleId;

                    /*Get Other Records */
                    short userTypeID = Convert.ToInt16(ddlRoleType.SelectedValue);
                    /*Insert To DataBase*/
                    UserRole userRole = new UserRole();
                    userRole.RoleId = roleID;
                    userRole.UserTypeId = userTypeID;
                    userRole.Status = 1;
                    structureFacade.InsertUserRole(userRole);

                    Response.Redirect("~/UserManagement/AssignPermissions.aspx?RoleId=" + roleID.ToString());
                }
                else
                {
                    MassageSummery("IIS505");
                }
            }
            catch
            {
                MassageSummery("IIS502");
            }

        }

        protected void ddlRoleType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetDataSourceToTypes(List<UserType> userTypes)
        {
            try
            {
                ddlRoleType.DataSource = userTypes;
                ddlRoleType.DataTextField = "UserTypeName";
                ddlRoleType.DataValueField = "UserTypeId";
                ddlRoleType.DataBind();


                InsertDDLItems(ddlRoleType, "Select...", 0);
            }
            catch (Exception)
            {

            }

        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            try
            {
                ddl.Items.Insert(Position, Text);
            }
            catch (Exception)
            {

            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/ViewRole.aspx");
        }

        public void SetCredentials(StringCollection credential)
        {
            try
            {
                foreach (string item in credential)
                {
                    if (item.ToLower() == "c")
                        btnCreateRole.Visible = true;

                }
            }
            catch (Exception)
            {

            }
            
        }
    }
}
